package cn.wipace.actionlib.inter.impl;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import cn.wipace.actionlib.UserDbHelper;
import cn.wipace.actionlib.activity.RadarMapActivity;
import cn.wipace.actionlib.config.Config;
import cn.wipace.actionlib.inter.UserInter;
import cn.wipace.actionlib.model.UploadResult;
import cn.wipace.actionlib.model.UserAction;
import cn.wipace.actionlib.model.UserScore;
import cn.wipace.actionlib.network.ApiService;
import cn.wipace.actionlib.network.HttpResultListener;
import cn.wipace.actionlib.util.L;
import cn.wipace.actionlib.util.NumberUtil;


/**
 * Created by Administrator on 2017/4/26.
 */

public class UserImpl implements UserInter {

    public static double STACK_LOW = 0.09;
    public static double STACK_MIDDLE = 0.5;
    public static double STACK_HIGH = 0.8;

    UserDbHelper mDbHelper;

    public UserImpl(Context context) {
        mDbHelper = UserDbHelper.getInstance(context);
    }


    @Override
    public void saveUserActionData(float t0, float t1, float t2, float t3, float t4, float t5) {
        UserAction userAction = new UserAction(t0, t1, t2, t3, t4, t5);
        mDbHelper.insertAction(userAction);
    }

    @Override
    public void saveUserActionData(long userId, float t0, float t1, float t2, float t3, float t4, float t5, String packageName, long gameStartTime) {
        UserAction userAction = new UserAction(userId, packageName, t0, t1, t2, t3, t4, t5, gameStartTime);
        mDbHelper.insertAction(userAction);
    }

    @Override
    public List<UserAction> findUserActionData() {
        return mDbHelper.queryActionAll();
    }

    @Override
    public void saveUserScoreData(long uid, float mindFullness, float exerciseAbility, float stressResponse, float coordination, float gameScore, String packageName, short uploadResult, long gameStartTime) {
        UserScore userScore = new UserScore(uid, mindFullness, exerciseAbility, stressResponse, coordination, gameScore, packageName, uploadResult, gameStartTime);
        mDbHelper.insertScore(userScore);
    }

    @Override
    public void saveUserScoreData(long uid, float mindFullness, float exerciseAbility, float stressResponse, float coordination, float gameScore, String packageName, long gameStartTime) {
        UserScore userScore = new UserScore(uid, mindFullness, exerciseAbility, stressResponse, coordination, gameScore, packageName, gameStartTime);
        mDbHelper.insertScore(userScore);
    }

    @Override
    public List<UserScore> findScoreTop10(long userId) {
        return mDbHelper.queryScoreTop10(userId);
    }

    @Override
    public List<UserScore> findScoreTop10(long userId, long gameStartTime) {
        return mDbHelper.queryScoreTop10(userId, gameStartTime);
    }

    @Override
    public UserScore findLatestScore(long userId) {
        return mDbHelper.queryScoreLatest(userId);
    }

    @Override
    public void openRadarMapActivity(Context context, String packageName, UserScore score) {
        Intent intent = new Intent(context, RadarMapActivity.class);
        intent.putExtra("pn", packageName);
        Bundle bundle = new Bundle();
        bundle.putSerializable("score", score);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    public List<UserAction> findUserActionByUidAndStartTime(long uid, long gameStartTime) {
        return mDbHelper.queryActionByStartTime(uid, gameStartTime);
    }

    @Override
    public void updateStackGameStatus(long scoreId, short gameStatus) {
        mDbHelper.updateScoreGameStatue(scoreId, gameStatus);
    }

    @Override
    public String stackEvaluateResult(UserAction userAction) {
        float t0 = 0f;
        float t1 = 0f;
        float t2 = 0f;
        float t3 = 0f;
        float t4 = 0f;
        float t5 = 0f;
        if (null != userAction) {
            t0 = userAction.getT0();
            t1 = userAction.getT1();
            t2 = userAction.getT2();
            t3 = userAction.getT3();
            t4 = userAction.getT4();
            t5 = userAction.getT5();
        }
       /* if (t5 < STACK_LOW) {//不作统计
            return "Z";
        } else {
            if (t5 >= ((t1 + t2) * STACK_MIDDLE) && t5 < t2) {//A
                Config.PreA++;
                if (t5 >= (t2 * 0.95) && (t5 < (t2 * 1.05))) {
                    Config.PerfectA++;//完美的次数
                }
                return "PreA";
            } else if ((t5 >= t2) && t5 < ((t2 + t3) * STACK_MIDDLE)) {//A
                Config.AftA++;
                if (t5 >= (t2 * 0.95) && (t5 < (t2 * 1.05))) {
                    Config.PerfectA++;//完美的次数
                }
                return "AftA";
            } else if (t5 >= t1 && t5 < ((t1 + t2) * STACK_MIDDLE)) {//B
                Config.PreB++;
                return "PreB";
            } else if (t5 >= ((t3 + t2) * STACK_MIDDLE) && t5 < t3) {//B
                Config.AftB++;
                return "AftB";
            } else if (t5 >= t1 * STACK_HIGH && t5 < t1) {//C
                Config.PreC++;
                return "PreC";
            } else if (t5 >= t3 && t5 < (STACK_HIGH * t3 + (1 - STACK_HIGH) * t4)) {//C
                Config.AftC++;
                return "AftC";
            } else if (t5 <= (STACK_HIGH * t3 + (1 - STACK_HIGH) * t4) && t5 < t4) {//D
                Config.AftD++;
                return "AftD";
            } else if (t5 >= STACK_LOW && t5 < t1 * STACK_HIGH) {//D
                Config.PreD++;
                return "PreD";
            } else {
                return "ZZ";
            }
        }*/

        if (t5 < STACK_LOW) {//不作统计
            return "Z";
        } else {
            if (t5 >= t1 && t5 < t2) {//A
                Config.PreA++;
                if (t5 >= (t2 * 0.9) && (t5 < (t2 * 1.1))) {
                    Config.PerfectA++;//完美的次数
                }
                return "PreA";
            } else if (t5 >= t2 && t5 < t3) {//A
                Config.AftA++;
                if (t5 >= (t2 * 0.9) && (t5 < (t2 * 1.1))) {
                    Config.PerfectA++;//完美的次数
                }
                return "AftA";
            } else if (t5 >= t1 * 0.75 && t5 < t1) {//B
                Config.PreB++;
                return "PreB";
            } else if (t5 >= t3 && t5 < (t3 + (t4 - t3) * 0.25)) {//B
                Config.AftB++;
                return "AftB";
            } else if (t5 >= t1 * 0.5 && t5 < t1 * 0.75) {//C
                Config.PreC++;
                return "PreC";
            } else if (t5 >= (t3 + (t4 - t3) * 0.25) && t5 < (t3 + (t4 - t3) * 0.5)) {//C
                Config.AftC++;
                return "AftC";
            } else if (t5 >= 0 && t5 < t1 * 0.5) {//D
                Config.PreD++;
                return "PreD";
            } else if (t5 <= (t3 + (t4 - t3) * 0.5) && t5 < t4) {//D
                Config.AftD++;
                return "AftD";
            } else {
                Config.PreD++;
                return "PreD";
            }
        }
    }

    @Override
    public UserScore stackEvluateAbility(UserAction userAction) {
        UserScore score = null;
        return score;
    }

    @Override
    public void uploadScoreData(UserScore score, HttpResultListener inter) {
        if (score != null) {
            UploadResult uploadResult = new UploadResult(score.getMindFullness() + "", score.getExerciseAbility() + "",
                    score.getEmergencyResponse() + "", score.getCoordination() + "", score.getGameScore() + "", score.getUid() + "",
                    score.getPackageName(), score.getGameStartTime() + "");
            ApiService.getInstance().uploadUserScoreData(uploadResult, inter);

        }
    }

    @Override
    public void getScoreData(long uid, HttpResultListener inter) {
        if (uid > 0) {
            ApiService.getInstance().getUserScoreData(inter, 16);
        }
    }


    /**
     * 运动能力得分
     *
     * @param gameScore
     */
    @Override
    public float getStackExerciseAbleScore(float gameScore) {
        Double scoreResult = 0d;
        if (gameScore > 0 && gameScore <= 5) {
            scoreResult = 62.0 + 50.0 * (gameScore / Config.STACK_SCORE_RESULT_THRESH);
        } else if (gameScore > 5 && gameScore <= 10) {
            scoreResult = 66.0 + 36.0 * (gameScore / Config.STACK_SCORE_RESULT_THRESH);
        } else if (gameScore > 10 && gameScore <= 20) {
            scoreResult = 75.0 + 20.0 * (gameScore / Config.STACK_SCORE_RESULT_THRESH);
        } else {
            scoreResult = 91.0 + (gameScore / Config.STACK_SCORE_RESULT_THRESH);
        }
        if (scoreResult > 99.0) {
            scoreResult = 95.3;
        }
        if ((scoreResult > 90) && (gameScore < 6)) {
            scoreResult = 0.9 * scoreResult;
        }
        L.e("kerrys", "运动能力得分==" + scoreResult);
        return scoreResult.floatValue();
    }

    @Override
    public String getStackScoreStr(float gameScore) {
        String range = "";
       /* float scoreResult = getStackExerciseAbleScore(gameScore);
        if (scoreResult >= 0 && scoreResult < 0.25) {
            range = "加油";
        } else if (scoreResult >= 0.25 && scoreResult < 0.5) {
            range = "较好";
        } else if (scoreResult > 0.5 && scoreResult < 0.75) {
            range = "良好";
        } else if (scoreResult >= 0.75 && scoreResult < 1) {
            range = "优秀";
        } else {
            range = "优秀";
        }*/
        return range;
    }

    /**
     * 反应能力得分
     *
     * @param gameScore
     * @return
     */
    @Override
    public float getStackRespAbleScore(float gameScore) {
        Config.PRE_COUNT = Config.PreA + Config.PreB + Config.PreC + Config.PreD;
        Config.AFT_COUNT = Config.AftA + Config.AftB + Config.AftC + Config.AftD;
        Config.MOVE_COUNT = Config.PRE_COUNT + Config.AFT_COUNT;

/*        Double stackA = (Config.PreA * 0.6 + Config.AftA * 0.4) * 4;
        Double stackB = (Config.PreB * 0.6 + Config.AftB * 0.4) * 3;
        Double stackC = (Config.PreC * 0.6 + Config.AftC * 0.4) * 2;
        Double stackD = (Config.PreD * 0.6 + Config.AftD * 0.4) * 1;

        float basicScore = (float) (stackA + stackB + stackC + stackD) / Config.MOVE_COUNT;//基础反应得分
        Config.BasiicScore = basicScore;
        float rewardScore = getStackExerciseAbleScore(gameScore);//奖励反应得分
        Config.RewardScore = rewardScore;
        float punishScore = (float) ((Config.AFT_COUNT * 0.5) / Config.MOVE_COUNT);//惩罚得分
        Config.PunishScore = punishScore;
        float stackReact = basicScore + rewardScore + punishScore;
        L.e("kerrys", "反应能力==" + stackReact);
        */
        float basicScore = 0.0f;
        int stackA = (Config.PreA + Config.AftA) * 4;
        int stackB = (Config.PreB + Config.AftB) * 3;
        int stackC = (Config.PreC + Config.AftC) * 2;
        int stackD = (Config.PreD + Config.AftD) * 1;
        basicScore = (float) (stackA + stackB + stackC + stackD) / (4 * Config.MOVE_COUNT) * 95;
        if ((basicScore > 90) && (gameScore < 5)) {
            basicScore = 9 * basicScore / 10;
        }
        if (basicScore > 99) {
            basicScore = 95.4f;
        }
        return basicScore;
    }

    /**
     * 专注能力得分
     *
     * @return
     */
    @Override
    public float getStackMindAbleScore(float gameScore) {
        Double stackFouce = 0d;
        /*if ((Config.PreA + Config.AftA) != 0) {
            Config.Fooucus1 = (float) (Config.PreA * 0.6 + Config.AftA * 0.4) / (Config.PreA + Config.AftA);
            Config.Foucus2 = (float) (Config.PerfectA) / (Config.PreA + Config.AftA);
            stackFouce = Config.Fooucus1 + Config.Foucus2;
        } else {
            if ((Config.PreB + Config.AftB) != 0) {
                Config.Fooucus1 = (float) (Config.PreA * 0.6 + Config.AftA * 0.4) / (Config.PreB + Config.AftB);
                stackFouce = Config.Fooucus1;
            } else {
                stackFouce = 0.5f;
            }
        }*/
        Config.PRE_COUNT = Integer.valueOf(Config.PreA.intValue() + Config.PreB.intValue() + Config.PreC.intValue() + Config.PreD.intValue());
        Config.AFT_COUNT = Integer.valueOf(Config.AftA.intValue() + Config.AftB.intValue() + Config.AftC.intValue() + Config.AftD.intValue());
        Config.MOVE_COUNT = Integer.valueOf(Config.PRE_COUNT.intValue() + Config.AFT_COUNT.intValue());
        int stackA = Config.PreA + Config.AftA;
        int stackD = Config.PreD + Config.AftD;
        int stackC = Config.PreC + Config.AftC;
        int stackB = Config.PreB + Config.AftB;

        double stackADQ = (double) (stackA + stackD) / Config.MOVE_COUNT;
        double stackBCQ = (double) (stackB + stackC) / Config.MOVE_COUNT;
        double scoreQ = (double) gameScore / Config.STACK_SCORE_RESULT_THRESH;
        //stackFouce = (stackADQ * (stackA + stackD) / Config.MOVE_COUNT + (1 - stackADQ) * gameScore / Config.STACK_SCORE_RESULT_THRESH) * 100;
        stackFouce = (stackADQ * (1 - scoreQ) + scoreQ) * 95;
        if ((stackA + stackD) == Config.MOVE_COUNT) {
            stackFouce = (0.9 * (stackA + stackD) / Config.MOVE_COUNT) * 95;
        }
        if ((stackA + stackD) == 0) {
            stackFouce = (stackBCQ) * 95;
        }
        if (stackFouce > 99) {
            stackFouce = 95.5;
        }
        if ((stackFouce > 90) && (gameScore < 5)) {
            stackFouce = 9 * stackFouce / 10;
        }
        Log.e("kerrys", "stackA" + stackA + "stackD" + stackD + "gamescore" + (gameScore / Config.STACK_SCORE_RESULT_THRESH));
        return stackFouce.floatValue();
    }

    /**
     * 协调能力得分
     *
     * @return
     */
    @Override
    public float getStackCoordAbleScore(float gameScore) {
        Double stackCoorder = 0d;
/*        if ((Config.PreA + Config.PreB + Config.AftA + Config.AftB) != 0) {
            Config.Coord1 = (float) ((Config.PreA + Config.PreB) * 0.6 + (Config.AftA + Config.AftB) * 0.4) / (Config.PreA + Config.PreB + Config.AftA + Config.AftB);
            Config.Coord2 = (float) (Config.PerfectA) / (Config.PreA + Config.AftA);
            stackCoorder = Config.Coord1 + Config.Coord2;
        } else {
            stackCoorder = 0.5f;
        }*/
        Config.PRE_COUNT = Config.PreA + Config.PreB + Config.PreC + Config.PreD;
        Config.AFT_COUNT = Config.AftA + Config.AftB + Config.AftC + Config.AftD;
        Config.MOVE_COUNT = Config.PRE_COUNT + Config.AFT_COUNT;
        int pre = Config.PreA * 4 + Config.PreB * 3 + Config.PreC * 2 + Config.PreD;
        int aft = Config.AftA * 4 + Config.AftB * 3 + Config.AftC * 2 + Config.AftD;
        double preCoordAble = (double) pre / Config.MOVE_COUNT;
        double aftCoordAble = (double) aft / Config.MOVE_COUNT;
        Double deltCoordAble = (preCoordAble - aftCoordAble) * 0.25;
        Config.Default_CoordAble = deltCoordAble;
        if (deltCoordAble >= 0) {
            stackCoorder = 60 + Math.abs(deltCoordAble) * 30;
            if (gameScore > 10) {
                stackCoorder = 65 + Math.abs(deltCoordAble) * 30;
            }
            if (deltCoordAble < 0.25) {
                if (gameScore < 10) {
                    stackCoorder = 60 + Math.abs(deltCoordAble) * 36 + 10 * gameScore / Config.STACK_SCORE_RESULT_THRESH;
                } else {
                    stackCoorder = 65 + Math.abs(deltCoordAble) * 32 + 6 * gameScore / Config.STACK_SCORE_RESULT_THRESH;
                }
            }
        } else {
            stackCoorder = 60 + Math.abs(deltCoordAble) * 25;
            if (gameScore > 10) {
                stackCoorder = 65 + Math.abs(deltCoordAble) * 25;
            }
            if (Math.abs(deltCoordAble) < 0.25) {
                if (gameScore < 10) {
                    stackCoorder = 55 + Math.abs(deltCoordAble) * 35 + 10 * gameScore / Config.STACK_SCORE_RESULT_THRESH;
                } else {
                    stackCoorder = 60 + Math.abs(deltCoordAble) * 32 + 6 * gameScore / Config.STACK_SCORE_RESULT_THRESH;
                }
            }
        }
        if ((stackCoorder > 90) && (gameScore < 5)) {
            stackCoorder = 9 * stackCoorder / 10;
        }
        if (stackCoorder > 99) {
            stackCoorder = 95.6;
        }
        L.e("kerrys", "协调能力==" + stackCoorder);
        return stackCoorder.floatValue();
    }

    /**
     * 清空常量
     */
    @Override
    public void resetConfiValue() {
        Config.PreA = 0;
        Config.AftA = 0;
        Config.PreB = 0;
        Config.AftB = 0;
        Config.PreC = 0;
        Config.AftC = 0;
        Config.PreD = 0;
        Config.AftD = 0;
        Config.PerfectA = 0;//完美的次数

        Config.MOVE_COUNT = 0;//有效动作次数(A+B+C+D)
        Config.PRE_COUNT = 0;//PreA动作次数(preA+preB+preC+preD)
        Config.AFT_COUNT = 0;//Aft动作次数(aftA+aftB+aftC+aftD)

        Config.BasiicScore = 0f;//
        Config.RewardScore = 0f;//
        Config.PunishScore = 0f;//
        Config.Fooucus1 = 0f;//
        Config.Foucus2 = 0f;//
        Config.Coord1 = 0f;//
        Config.Coord2 = 0f;//
        Config.Default_CoordAble = 0d;
    }

    @Override
    public String getTestValue() {
        String testValue = "";
        float temp1 = Config.PreA + Config.PreB + Config.PreC + Config.PreD;
        String PreMoveCount = "PreMoveCount:" + temp1 + ",";
        float temp2 = Config.AftA + Config.AftB + Config.AftC + Config.AftD;
        String AftMoveCount = "AfterMoveCouont:" + temp2 + ",";
        float temp3 = temp1 + temp2;
        String moveCount = "MoveCount:" + temp3 + ",";
        String BasicScore = "反应基础得分:" + Config.BasiicScore + ",";
        String RewardScore = "反应奖励得分:" + Config.RewardScore + ",";
        String PunishScore = "反应惩罚得分" + Config.PunishScore + ",";
        String Foucus1 = "专注第一步:" + Config.Fooucus1 + ",";
        String Foucus2 = "专注第二步:" + Config.Foucus2 + ",";
        String FoucusDefault = " Config.Default_CoordAble:" + Config.Default_CoordAble + ",";
        String Coorder1 = "协调第一步:" + Config.Coord1 + ",";
        String Coorder2 = "协调第二步:" + Config.Coord2 + ",";

        testValue = moveCount + PreMoveCount + AftMoveCount + BasicScore + RewardScore + PunishScore + Foucus1 + Foucus2 + FoucusDefault + Coorder1 + Coorder2;
        return testValue;
    }

    @Override
    public String getPreAftValue() {
        String preAft = "preA=" + Config.PreA + ",aftA=" + Config.AftA + ",preB=" + Config.PreB + ",aftB=" + Config.AftB + "" +
                ",preC=" + Config.PreC + ",aftC=" + Config.AftC + ",preD=" + Config.PreD + ",aftD=" + Config.AftD;
        return preAft;
    }

    @Override
    public UserScore getAvgScore(List<UserScore> userScores) {
        UserScore userScore = new UserScore();
        int size = userScores.size();
        float exerciseScore = 0.0f;
        float mindScore = 0.0f;
        float emergencyScore = 0.0f;
        float coorderScore = 0.0f;
        long userId = 0l;
        long gameStartTime = 0l;
        String pn = "";
        for (UserScore us : userScores) {
            exerciseScore = exerciseScore + us.getExerciseAbility();
            mindScore = mindScore + us.getMindFullness();
            emergencyScore = emergencyScore + us.getEmergencyResponse();
            coorderScore = coorderScore + us.getCoordination();
            pn = us.getPackageName();
            userId = us.getUid();
            gameStartTime = us.getGameStartTime();
        }
        exerciseScore = NumberUtil.keepPrecision(exerciseScore / size, 2);
        mindScore = NumberUtil.keepPrecision(mindScore / size, 2);
        emergencyScore = NumberUtil.keepPrecision(emergencyScore / size, 2);
        coorderScore = NumberUtil.keepPrecision(coorderScore / size, 2);
        userScore.setExerciseAbility(exerciseScore);
        userScore.setMindFullness(mindScore);
        userScore.setEmergencyResponse(emergencyScore);
        userScore.setCoordination(coorderScore);
        userScore.setPackageName(pn);
        userScore.setUid(userId);
        userScore.setGameStartTime(gameStartTime);
        return userScore;
    }

    @Override
    public double getFinalScore(int score) {
        double result = 0;
        DecimalFormat df = new DecimalFormat("#.00");
        if (score >= 0 && score < 6) {//50-60
            result = 50 + (score) / 5.0 * 10;
        } else if (score >= 6 && score < 11) {//60-70
            result = 60 + (score - 6) / 5.0 * 10;
        } else if (score >= 11 && score < 16) {//70-78
            result = 70 + (score - 11) / 5.0 * 8;
        } else if (score >= 16 && score < 20) {//79-84
            result = 79 + (score - 16) / 5.0 * 6;
        } else if (score >= 20 && score < 25) {//84-90
            result = 84 + (score - 20) / 5.0 * 6;
        } else if (score >= 25 && score < 50) {
            result = 89 + (score - 25) / 25.0 * 10;
        } else {
            result = 99;
        }
        return new Double(df.format(result));
    }

    /**
     * 获得tom时间得分
     *
     * @param score
     * @return
     */
    @Override
    public double getTomScore(double score) {
        double Rt = getRt(score);
        double Rp = getRp();
        double x1 = Rt / (Rt + Rp);
        double x2 = 1 - x1;
        double result = (x1 * Rt + x2 * Rp) * 100.0;
        return result;
    }

    /**
     * 获取根据时间判读的得分
     *
     * @param score
     * @return
     */
    public double getRt(double score) {
        double result = 0;
        if (score >= 20 && score < 25) {
            result = 90 + (score - 20) / 5.0 * 1.2;
        } else if (score >= 25 && score < 30) {
            result = 80 + (score - 25) / 5.0 * 1.4;
        } else if (score >= 30 && score < 35) {
            result = 70 + (score - 30) / 5.0 * 1.6;
        } else if (score >= 35 && score < 40) {
            result = 60 + (score - 35) / 5.0 * 1.8;
        } else if (score >= 40 && score <= 45) {
            result = 50 + (score - 40) / 5.0 * 2;
        }
        double Rt = result / 100.0;
        return Rt;
    }

    /**
     * tom频率判断的得分
     *
     * @return
     */
    public double getRp() {
        double Rp = 0.0;
        double totalT0 = Config.H0 + Config.H1 + Config.H2 + Config.H3 + Config.H4 + Config.H5;
        double p0 = Config.H0 / totalT0 * 100.0;
        double p1 = Config.H1 / totalT0 * 100.0;
        double p2 = Config.H2 / totalT0 * 100.0;
        double p3 = Config.H3 / totalT0 * 100.0;
        double p4 = Config.H4 / totalT0 * 100.0;
        double p5 = Config.H5 / totalT0 * 100.0;

        Rp = (getPCount(p0, 0) + getPCount(p1, 1) + getPCount(p2, 2) +
                getPCount(p3, 3) + getPCount(p4, 4) + getPCount(p5, 5)) / 21.0;
        return Rp;
    }

    public int getPCount(double p0, int type) {
        int pCount = 0;
        if (p0 >= 85 && p0 <= 99) {
            switch (type) {
                case 0:
                    pCount = 1;
                    break;
                case 1:
                    pCount = 2;
                    break;
                case 2:
                    pCount = 4;
                    break;
                case 3:
                    pCount = 5;
                    break;
                case 4:
                    pCount = 3;
                    break;
                case 5:
                    pCount = 6;
                    break;
            }
        } else if (p0 >= 70 && p0 < 85) {
            switch (type) {
                case 0:
                    pCount = 5;
                    break;
                case 1:
                    pCount = 6;
                    break;
                case 2:
                    pCount = 1;
                    break;
                case 3:
                    pCount = 3;
                    break;
                case 4:
                    pCount = 4;
                    break;
                case 5:
                    pCount = 2;
                    break;
            }
        } else if (p0 >= 55 && p0 < 70) {
            switch (type) {
                case 0:
                    pCount = 3;
                    break;
                case 1:
                    pCount = 4;
                    break;
                case 2:
                    pCount = 2;
                    break;
                case 3:
                    pCount = 6;
                    break;
                case 4:
                    pCount = 5;
                    break;
                case 5:
                    pCount = 1;
                    break;
            }
        } else if (p0 >= 40 && p0 < 55) {
            switch (type) {
                case 0:
                    pCount = 2;
                    break;
                case 1:
                    pCount = 1;
                    break;
                case 2:
                    pCount = 3;
                    break;
                case 3:
                    pCount = 4;
                    break;
                case 4:
                    pCount = 6;
                    break;
                case 5:
                    pCount = 5;
                    break;
            }
        } else if (p0 >= 20 && p0 < 40) {
            switch (type) {
                case 0:
                    pCount = 4;
                    break;
                case 1:
                    pCount = 5;
                    break;
                case 2:
                    pCount = 6;
                    break;
                case 3:
                    pCount = 1;
                    break;
                case 4:
                    pCount = 2;
                    break;
                case 5:
                    pCount = 3;
                    break;
            }
        } else if (p0 >= 1 && p0 < 20) {
            switch (type) {
                case 0:
                    pCount = 6;
                    break;
                case 1:
                    pCount = 3;
                    break;
                case 2:
                    pCount = 5;
                    break;
                case 3:
                    pCount = 2;
                    break;
                case 4:
                    pCount = 1;
                    break;
                case 5:
                    pCount = 4;
                    break;
            }
        }
        return pCount;
    }

    /**
     * 求分段时间差的和
     *
     * @param duration
     */
    @Override
    public void totalDuration(double duration) {
        if (duration >= 1.1 && duration <= 3) {
            Config.H0 += 1 / duration;
        } else if (duration >= 0.75 && duration < 1.1) {
            Config.H1 += 1 / duration;
        } else if (duration >= 0.4 && duration < 0.75) {
            Config.H2 += 1 / duration;
        } else if (duration >= 0.28 && duration < 0.4) {
            Config.H3 += 1 / duration;
        } else if (duration >= 0.22 && duration < 0.28) {
            Config.H4 += 1 / duration;
        } else if (duration >= 0.17 && duration < 0.22) {
            Config.H5 += 1 / duration;
        }
    }

    @Override
    public void resetDuartion() {
        Config.H0 = 0d;
        Config.H1 = 0d;
        Config.H2 = 0d;
        Config.H3 = 0d;
        Config.H4 = 0d;
        Config.H5 = 0d;
    }

    @Override
    public String showDuration() {
        String str = "小于1Hz=" + Config.H0 + " 1Hz=" + Config.H1 + " 2Hz=" + Config.H2 +
                " 3Hz=" + Config.H3 + " 4Hz=" + Config.H4 + " 5Hz=" + Config.H5;

        return str;
    }


    @Override
    public Uri saveScoreByProvider(Uri uri, String gameScore, Context context) {
        ContentResolver resolver = context.getContentResolver();
        ContentValues values = new ContentValues();
        values.put("game_score", gameScore);
        return resolver.insert(uri, values);
    }


    @Override
    public List<UserScore> getScoreByProvider(Uri uri, Context context) {
        List<UserScore> list = new ArrayList<>();
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = resolver.query(uri, null, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                UserScore userScore = new UserScore();
                float score = cursor.getFloat(6);
                long gameStartTime = cursor.getLong(9);
                userScore.setGameScore(score);
                userScore.setGameStartTime(gameStartTime);
                list.add(userScore);
            }
        }
        return list;
    }


}
